<script setup>
import { userLoginService, userRegisterService } from '@/api/user'
import { useUserStore } from '@/stores/index'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'

const isRegister = ref(true)
const form = ref()
const userStore = useUserStore()
const router = useRouter()

// 整个注册信息（登录共用）
const allForm = ref({
  username: '',
  password: '',
  repassword: ''
})
// 校验规则
const rules = {
  username: [
    // 非空校验
    { required: true, message: '请输入用户名', trigger: 'blur' },
    // 长度校验
    { min: 6, max: 10, message: '用户名是6-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,10}$/,
      message: '密码是6-10位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,10}$/,
      message: '密码是6-10位的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value === allForm.value.password) {
          callback()
        } else {
          callback(new Error('请输入相同的密码'))
          // console.log(value)
        }
      },
      trigger: 'blur'
    }
  ]
}
// 注册事件
const register = async () => {
  await form.value.validate()
  await userRegisterService(allForm.value)
  isRegister.value = true
  ElMessage.success('注册成功！')
}
// 登录事件
const login = async () => {
  await form.value.validate()
  const { data } = await userLoginService(allForm.value)
  userStore.setToken(data.token)
  ElMessage.success('登陆成功！')
  router.push('/')
}
// 监听isRegister变化，清空输入
watch(isRegister, () => {
  allForm.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="login-left"></el-col>
    <el-col :span="6" :offset="3" class="login-right">
      <el-form v-if="isRegister" :model="allForm" :rules="rules" ref="form">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="allForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
            style="height: 50px"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="allForm.password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            style="height: 50px"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="login-item3">
            <el-checkbox> 记住我 </el-checkbox>
            <el-link type="primary">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button @click="login" class="login-but" type="primary"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link @click="isRegister = false" type="info" :underline="false"
            >注册 →
          </el-link>
        </el-form-item>
      </el-form>
      <el-form v-else :model="allForm" :rules="rules" ref="form">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="allForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
            style="height: 50px"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="allForm.password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            style="height: 50px"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="allForm.repassword"
            :prefix-icon="Lock"
            placeholder="请再次输入密码"
            style="height: 50px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="register" class="register-but" type="primary"
            >注册</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link @click="isRegister = true" type="info" :underline="false"
            >← 返回
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
.login-page {
  height: 100vh;
}
.login-left {
  background:
    url('@/assets/logo2.png') no-repeat 50% center / 240px auto,
    url('@/assets/login_bg.jpg') no-repeat center / cover;
  border-radius: 0 20px 20px 0;
}
.login-right {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.login-right h1 {
  margin-bottom: 20px;
}
.login-item3 {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.login-but {
  font-size: 22px;
  line-height: 40px;
  width: 100%;
  height: 50px;
}
.register-but {
  font-size: 22px;
  line-height: 40px;
  width: 100%;
  height: 50px;
}
</style>
